import util from '@/libs/util';
import Cookies from 'js-cookie';
import getDate from '@/util/getDate.js';
import CONSTANTS from '@/config/constants';

export default {
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                width: 60,
                align: 'center',
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
                title: '机构名称',
                key: 'orgName'
            }, {
                title: '管理账号',
                key: 'userName'
            }, {
                title: '联系人',
                key: 'orgContacts'
            }, {
                title: '联系电话',
                key: 'orgPhone'
            }, {
                title: '地址',
                key: 'orgAddress'
            }, {
                title: '缴费套餐',
                key: 'chargeid',
                render: (h, params) => {
                    return h('a', {
                        on: {
                            click: () => {
                                this.formData.chargeid = params.row.chargeid;
                                this.chargeDetail();
                            }
                        }
                    }, params.row.charge ? params.row.charge.name : '');
                }
            }, {
                title: '状态',
                key: 'type',
                width: 100,
                render: (h, params) => {
                    let texts = '';
                    if (params.row.type === 1) {
                        texts = '待审核';
                    } else if (params.row.type === 2) {
                        texts = '已审核';
                    }
                    return h('div', {}, texts);
                }
            }, {
                title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.update(params.row.id);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px',
                                display: (eval('(' + Cookies.get('baseUser') + ')').type === 1 ? 'inline' : 'none')
                            },
                            on: {
                                click: () => {
                                    this.audit(params.row);
                                }
                            }
                        }, '审核'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.orgid = params.row.id;
                                    this.prizesSelect();
                                }
                            }
                        }, '奖状查询')
                    ]);
                }
            }],
            prizesColumns: [
                {title: '主题', key: 'themeid', render: (h, params) => { return h('div', {}, params.row.theme.name); }},
                {title: '奖项', key: 'diplomasid', render: (h, params) => { return h('div', {}, params.row.diplomas.name); }},
                {title: '获奖人', key: 'name', render: (h, params) => { return h('div', {}, params.row.member.realname); }},
                {title: '获奖人电话', key: 'phone', render: (h, params) => { return h('div', {}, params.row.member.phone); }},
                {
                    title: '奖状生成时间',
                    key: 'createtime',
                    render: (h, params) => { return h('div', {}, getDate.getDate(params.row.createtime, 'time')); }
                },
                {title: '下载次数', key: 'downloadnum'},
                {title: '操作',
                    key: 'action',
                    width: 200,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.prizeModal = true;
                                        this.imgpath = CONSTANTS.SERVER_HOST + params.row.imgpath;
                                    }
                                }
                            }, '查看奖状')
                        ]);
                    }
                }
            ],
            prizesData: [],
            data: [],
            formData: {},
            query: {
                orgName: '',
                orgContacts: '',
                orgPhone: '',
                type: ''
            },
            viewModal: false,
            prizesModal: false,

            total: 0,
            pageIndex: 1,
            pageSize: 10,

            prizesTotal: 0,
            prizesIndex: 1,
            prizesSize: 10,

            chargeList: [],
            chargeData: {},
            chargeModel: false,

            prizeModal: false,
            imgpath: '',
            orgid: ''
        };
    },
    mounted () {
        this.loadList();
        util.ajax.post('/busine/charge/findAll', util.toFormData()).then(res => {
            this.chargeList = res.data.data;
        });
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/organize/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                orgName: this.query.orgName,
                orgContacts: this.query.orgContacts,
                orgPhone: this.query.orgPhone,
                type: (this.query.type ? this.query.type : ''),
                userID: eval('(' + Cookies.get('baseUser') + ')').id
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        update (id) {
            util.ajax.post('/busine/organize/detail', util.toFormData({
                id: id
            })).then(res => {
                this.formData = res.data.data;
                this.viewModal = true;
            });
        },
        save () {
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            formData['createTime'] = getDate.getDate(formData['createTime'], 'time');
            formData['updatetime'] = getDate.getDate(formData['updatetime'], 'time');
            formData['audittime'] = getDate.getDate(formData['audittime'], 'time');
            util.ajax.post('/busine/organize/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.viewModal = false;
                    this.$Message.success('修改成功！');
                    this.loadList();
                } else {
                    this.$Message.warning(res.data.message || '未知错误！');
                }
            });
        },
        audit (obj) {
            if (obj.type === 2) {
                this.$Modal.warning({
                    content: '该机构已通过审核，请不要重复审核！'
                })
                return;
            }
            this.$Modal.confirm({
                content: '是否确认审核？',
                onOk: () => {
                    let formData = {};
                    for (let key in obj) {
                        formData[key] = obj[key];
                    }
                    formData['createTime'] = getDate.getDate(formData['createTime'], 'time');
                    formData['updatetime'] = getDate.getDate(formData['updatetime'], 'time');
                    formData['audittime'] = getDate.getDate(formData['audittime'], 'time');
                    formData['userName'] = eval('(' + Cookies.get('baseUser') + ')').name;
                    util.ajax.post('/busine/organize/examine', util.toFormData(formData)).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('审核成功！');
                            this.loadList();
                        } else {
                            this.$Message.warning(res.data.message || '未知错误！');
                        }
                    });
                }
            })
        },
        refresh () {
            this.query = {
                orgName: '',
                orgContacts: '',
                orgPhone: '',
                type: ''
            };
            this.loadList();
        },
        prizesSelect () {
            util.ajax.post('/busine/prizes/list', util.toFormData({
                page: this.prizesIndex,
                size: this.prizesSize,
                orgid: this.orgid
            })).then(res => {
                this.prizesData = res.data.data.list;
                this.prizesTotal = res.data.data.total;
                this.prizesModal = true;
            });
        },
        chargeDetail () {
            util.ajax.post('/busine/charge/detail', util.toFormData({
                id: this.formData.chargeid
            })).then(res => {
                this.chargeData = res.data.data;
                this.chargeModel = true;
            });
        }
    }
}